package com.huang.auth.endpoint;

import com.alibaba.fastjson.JSON;
import com.huang.auth.entity.to.TokenInfoTO;
import com.huang.auth.entity.vo.ValidationTokenVO;
import com.huang.auth.service.AuthTokenService;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResponseEnum;
import com.huang.entity.result.ResultBuilder;
import com.huang.utils.entity.to.TokenValResultTO;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author HuangShen
 * @Description
 * @create 2021-10-04 14:56
 */
@RestController(value = "authTokenApi")
public class AuthTokenEndpoint implements AuthTokenApi {

    @Resource
    private AuthTokenService authTokenService;


    @Override
    public HStringResult<ValidationTokenVO> validationToken(String token) {



        TokenValResultTO tokenRes = authTokenService.validationToken(token);
        if (tokenRes.getCode() == 200) {
            String tokenInfo =  tokenRes.getTokenInfo();
            TokenInfoTO tokenInfoTO = JSON.parseObject(tokenInfo, TokenInfoTO.class);
            ValidationTokenVO validationTokenVO = new ValidationTokenVO();
            validationTokenVO.setUserAccount(tokenInfoTO.getUserAccount());
            validationTokenVO.setRoleNames(tokenInfoTO.getRoleNames());
            validationTokenVO.setUserId(tokenInfoTO.getUserId());
            return ResultBuilder.successHSingle(validationTokenVO);
        }
        return ResultBuilder.failHSingle(ResponseEnum.ERROR_ACCESS_TOKEN_TIMEOUT,tokenRes.getMsg());
    }
}
